﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.Activities.Rules;
using RelationExpertSystem.Controllers;
using System.Diagnostics;

namespace RelationExpertSystem
{
    public partial class RelationshipForm : Form
    {
        RelationController _controller;

        public RelationshipForm(RelationController controller)
        {
            InitializeComponent();
            this._controller = controller;

            _controller.NewFactsLoaded += new EventHandler<FactsEventArgs>(HandleSession_NewFactsLoaded);
            _controller.FactsUpdated += new EventHandler<FactsEventArgs>(HandleSession_FactsUpdated);
            _controller.NewInference += new EventHandler<MessageEventArgs>(HandleSession_NewInference);
            _controller.RulesUpdated += new EventHandler(HandleRulesUpdated);
        }

        private void HandleRulesUpdated(object sender, EventArgs args)
        {
            PopulateRulesListBox();
        }

        private void PopulateRulesListBox()
        {
            lbRules.Items.Clear();
            foreach (Rule rule in _controller.GetRules())
            {
                lbRules.Items.Add(rule.Name);
                //Debug.WriteLine("Rule: " + rule.Name);
            }
        }

        void HandleSession_NewFactsLoaded(object sender, FactsEventArgs e)
        {
            lbFacts.Items.Clear();
            foreach (string factMessage in e.FactMessages)
                lbFacts.Items.Add(factMessage);
            lbInferences.Items.Clear();
        }

        void HandleSession_FactsUpdated(object sender, FactsEventArgs e)
        {
            lbFacts.Items.Clear();
            foreach (string factMessage in e.FactMessages)
                lbFacts.Items.Add(factMessage);
        }
        
        void HandleSession_NewInference(object sender, MessageEventArgs e)
        {
            lbInferences.Items.Add(e.Message);
        }

        private void HandleBtnLoadRules_Click(object sender, EventArgs e)
        {
            _controller.LoadRules();
        }

        private void HandleBtnEditRules_Click(object sender, EventArgs e)
        {
            _controller.EditRules();
            PopulateRulesListBox();
        }

        private void HandleBtnInfer_Click(object sender, EventArgs e)
        {
            _controller.Infer();
        }

        private void HandleBtnLoadFacts_Click(object sender, EventArgs e)
        {
            _controller.LoadFacts();
        }

        private void btnEditFacts_Click(object sender, EventArgs e)
        {
            _controller.EditFacts();
        }

        private void HandleBtnNewFacts_Click(object sender, EventArgs e)
        {
            _controller.NewFacts();
        }

        private void HandleBtnConverse_Click(object sender, EventArgs e)
        {
            // use BackwardChainer to build rules graph
            // pop up user dialog
            // wait for user to enter question
            // convert question to logic of THEN condition
            // pass THEN condition to rules graph to get IF conditions
            // recursively pass this IF conditions as THEN conditions to rules graph
            // get to point where IF is known
            // forward chain to conclusion
        }
    }
}
